package com.xy.p2p.action.admin;



import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.octo.captcha.service.CaptchaServiceException;
import com.octo.captcha.service.image.ImageCaptchaService;
import com.xy.common.page.Pagination;
import com.xy.common.util.DictUtils;
import com.xy.common.security.BadCredentialsException;
import com.xy.common.security.UsernameNotFoundException;
import com.xy.common.web.CookieUtils;
import com.xy.common.web.RequestUtils;
import com.xy.core.web.WebErrors;

import com.xy.p2p.model.Tfinanceaudit;
import com.xy.p2p.service.TfinanceauditService;



@Controller
public class TfinanceauditAction{
	private static final Logger log = LoggerFactory.getLogger(TfinanceauditAction.class);
	public TfinanceauditAction() {
	}
 
 
 	/**
	 * 融资审核表新增页面
	 * 
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_tfinanceauditAdd.do")
	public String add(HttpServletRequest request, ModelMap model) {
		model.addAttribute("tfinanceaudit", new Tfinanceaudit());
        setDict(model);
		return "tfinanceauditEdit";
	}

	/**
	 * 融资审核表新增保存
	 * 
	 * @param tfinanceaudit
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("/o_tfinanceauditSave.do")
	public String save(Tfinanceaudit tfinanceaudit, HttpServletRequest request, ModelMap model) {
		WebErrors errors = validateSave(tfinanceaudit, request);
		if (errors.hasErrors()) {
			return errors.showErrorPage(model);
		}
		tfinanceaudit = tfinanceauditService.save(tfinanceaudit);
		return "redirect:v_tfinanceauditList.do";
	}

	/**
	 * 融资审核表删除
	 * 
	 * @param ids
	 * @return
	 */
	@RequestMapping("o_tfinanceauditDelete.do")
	public String delete(long[] ids) {
		if (ids != null) {
			for (long id : ids) {
				tfinanceauditService.remove(id);
			}
		}
		return "redirect:v_tfinanceauditList.do";
	}

	/**
	 * 融资审核表编辑页面
	 * 
	 * @param faid
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_tfinanceauditEdit.do")
	public String edit(long faid, HttpServletRequest request, ModelMap model) {
		Tfinanceaudit tfinanceaudit = tfinanceauditService.get(faid);
		model.addAttribute("tfinanceaudit", tfinanceaudit);
        setDict(model);
		return "tfinanceauditEdit";
	}

	/**
	 * 融资审核表详情
	 * 
	 * @param faid
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_tfinanceauditDetail.do")
	public String detail(long faid, HttpServletRequest request, ModelMap model) {
		Tfinanceaudit tfinanceaudit = tfinanceauditService.get(faid);
		model.addAttribute("tfinanceaudit", tfinanceaudit);
        setDict(model);
		return "tfinanceauditView";
	}

	/**
	 * 融资审核表查询（分页）
	 * 
	 * @param tfinanceaudit
	 * @param pageNo
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_tfinanceauditList.do")
	public String listPage(Tfinanceaudit tfinanceaudit, Integer pageNo,
			HttpServletRequest request, ModelMap model) {
		if (pageNo == null)
			pageNo = 1;
		Pagination pagination = tfinanceauditService.getTfinanceauditByPage(toMap(tfinanceaudit),
				pageNo, CookieUtils.getPageSize(request));
		model.addAttribute("pagination", pagination);
        setDict(model);
		return "tfinanceauditList";
	}

	/**
	 * 融资审核表查询
	 * 
	 * @param tfinanceaudit
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_tfinanceauditList1.do")
	public String list(Tfinanceaudit tfinanceaudit, HttpServletRequest request, ModelMap model) {
		List<Tfinanceaudit> list = tfinanceauditService.getTfinanceaudit(toMap(tfinanceaudit));
		model.addAttribute("list", list);
        setDict(model);
		return "tfinanceauditList";
	}


 	/***
	 * 将页面输入信息转成map
	 * 
	 * @param tfinanceaudit
	 * @return
	 */	private Map<String,Object> toMap(Tfinanceaudit tfinanceaudit){
		Map<String,Object> map=new HashMap<String, Object>();
      		if(tfinanceaudit.getFaid()!=null&&!tfinanceaudit.getFaid().equals("")){
			map.put("faid", tfinanceaudit.getFaid());
		}
		if(tfinanceaudit.getTfinanceinfo()!=null&&!tfinanceaudit.getTfinanceinfo().equals("")){
			map.put("Tfinanceinfo", tfinanceaudit.getTfinanceinfo());
		}
		if(tfinanceaudit.getAuditresults1()!=null&&!tfinanceaudit.getAuditresults1().equals("")){
			map.put("auditresults1", tfinanceaudit.getAuditresults1());
		}
		if(tfinanceaudit.getTuserByReviewed1()!=null&&!tfinanceaudit.getTuserByReviewed1().equals("")){
			map.put("TuserByReviewed1", tfinanceaudit.getTuserByReviewed1());
		}
		if(tfinanceaudit.getRevieweddate1()!=null&&!tfinanceaudit.getRevieweddate1().equals("")){
			map.put("revieweddate1", tfinanceaudit.getRevieweddate1());
		}
		if(tfinanceaudit.getAuditopinion1()!=null&&!tfinanceaudit.getAuditopinion1().equals("")){
			map.put("auditopinion1", tfinanceaudit.getAuditopinion1());
		}
		if(tfinanceaudit.getAuditresults2()!=null&&!tfinanceaudit.getAuditresults2().equals("")){
			map.put("auditresults2", tfinanceaudit.getAuditresults2());
		}
		if(tfinanceaudit.getTuserByReviewed2()!=null&&!tfinanceaudit.getTuserByReviewed2().equals("")){
			map.put("TuserByReviewed2", tfinanceaudit.getTuserByReviewed2());
		}
		if(tfinanceaudit.getRevieweddate2()!=null&&!tfinanceaudit.getRevieweddate2().equals("")){
			map.put("revieweddate2", tfinanceaudit.getRevieweddate2());
		}
		if(tfinanceaudit.getAuditopinion2()!=null&&!tfinanceaudit.getAuditopinion2().equals("")){
			map.put("auditopinion2", tfinanceaudit.getAuditopinion2());
		}
		if(tfinanceaudit.getAuditresults3()!=null&&!tfinanceaudit.getAuditresults3().equals("")){
			map.put("auditresults3", tfinanceaudit.getAuditresults3());
		}
		if(tfinanceaudit.getTuserByReviewed3()!=null&&!tfinanceaudit.getTuserByReviewed3().equals("")){
			map.put("TuserByReviewed3", tfinanceaudit.getTuserByReviewed3());
		}
		if(tfinanceaudit.getRevieweddate3()!=null&&!tfinanceaudit.getRevieweddate3().equals("")){
			map.put("revieweddate3", tfinanceaudit.getRevieweddate3());
		}
		if(tfinanceaudit.getAuditopinion3()!=null&&!tfinanceaudit.getAuditopinion3().equals("")){
			map.put("auditopinion3", tfinanceaudit.getAuditopinion3());
		}
		if(tfinanceaudit.getTuserByLenders()!=null&&!tfinanceaudit.getTuserByLenders().equals("")){
			map.put("TuserByLenders", tfinanceaudit.getTuserByLenders());
		}
		if(tfinanceaudit.getLoandate()!=null&&!tfinanceaudit.getLoandate().equals("")){
			map.put("loandate", tfinanceaudit.getLoandate());
		}      
		return map;
	}
   
	/**
	 * 设置页面所需的数据字典
	 * @param model
	 */
	private void setDict(ModelMap model){
      model.addAttribute("auditresults1Map", DictUtils.getDictByType(DictUtils.TFINANCEAUDIT_AUDITRESULTS1));
      model.addAttribute("auditresults2Map", DictUtils.getDictByType(DictUtils.TFINANCEAUDIT_AUDITRESULTS2));
      model.addAttribute("auditresults3Map", DictUtils.getDictByType(DictUtils.TFINANCEAUDIT_AUDITRESULTS3));		
	}

	/**
	 * 数据校验
	 * @param model
	 */
	private WebErrors validateSave(Tfinanceaudit tfinanceaudit, HttpServletRequest request) {
		WebErrors errors = WebErrors.create(request);
		/**if (errors.ifOutOfLength(username, "username", 1, 100)) {
			return errors;
		}  */    
		return errors;
	}

   
      
	@Resource
	private TfinanceauditService tfinanceauditService;   
}